﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ProjectTask
{
    public partial class FrmTaskInfo : Form
    {
        ProjectCommon.ClassCommon cc;
        ClassTask ctTask;
        ProjectCommon.ClassCommon.OperateState ccosState;
        string ShopHideColumns0 = "自动编号,门店状态,店长电话,裱花电话,宽带号/光纤号,商户号,终端号,端口";
        //string ShopHideColumns1 = "自动编号,门店状态,店长电话,裱花电话,宽带号/光纤号,端口";
        string sTaskID;

        public FrmTaskInfo(ProjectCommon.ClassCommon aClassCommon, ProjectCommon.ClassCommon.OperateState ccos, string JobTaskID)
        {
            InitializeComponent();
            cc = aClassCommon;
            ccosState = ccos;
            sTaskID = JobTaskID;
        }

        private bool TaskNewInitial()
        {
            bool result = false;
            this.Text += "---添加";
            tbCreateTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            dtpPlannedFinishDate.Value = DateTime.Now.AddDays(1);
            tbPlannedFinishDate.Text = dtpPlannedFinishDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
            cbTaskState.Text = "待处理";
            tbOperater.Text = cc.dtLoginUser.Rows[0]["RealName"].ToString();
            ctTask = new ClassTask(cc.scKengee_IT_MIS);
            ctTask.TaskNewInitial();
            return result;
        }

        private bool TaskModifyInitial(string ID)
        {
            bool result = false;
            this.Text += "---修改";
            ctTask = new ClassTask(cc.scKengee_IT_MIS);
            result = ctTask.GetTask_ID(ID);
            ProjectCommon.ClassCommon.SetClass2Form(ctTask.dtTask, this.Controls);
            SetShop2dgv_FastCode(tbShopFastCode.Text);
            dtpPlannedFinishDate.Value = DateTime.Parse(tbPlannedFinishDate.Text);
            int NewRowIndex = dgvTaskInfo.Rows.Add();
            dgvTaskInfo.CurrentCell = dgvTaskInfo.Rows[NewRowIndex].Cells["任务分类"];
            dgvTaskInfo.CurrentRow.Cells["任务分类"].Value = tbTaskType.Text;
            dgvTaskInfo.CurrentRow.Cells["任务对象"].Value = tbTaskObject.Text;
            dgvTaskInfo.CurrentRow.Cells["任务描述"].Value = tbTaskDepict.Text;
            dgvTaskInfo.CurrentRow.Cells["任务备注"].Value = tbRemark.Text;
            GetFastInput(tbTaskType.Text);
            gbShopCtrl.Enabled = false;
            return result;
        }

        private bool TaskViewInitial(string ID)
        {
            bool result = false;
            this.Text += "---浏览";
            ctTask = new ClassTask(cc.scKengee_IT_MIS);
            result = ctTask.GetTask_ID(ID);
            ProjectCommon.ClassCommon.SetClass2Form(ctTask.dtTask, this.Controls);
            dtpPlannedFinishDate.Value = DateTime.Parse(tbPlannedFinishDate.Text);
            int NewRowIndex = dgvTaskInfo.Rows.Add();
            dgvTaskInfo.CurrentCell = dgvTaskInfo.Rows[NewRowIndex].Cells["任务分类"];
            dgvTaskInfo.CurrentRow.Cells["任务分类"].Value = tbTaskType.Text;
            dgvTaskInfo.CurrentRow.Cells["任务对象"].Value = tbTaskObject.Text;
            dgvTaskInfo.CurrentRow.Cells["任务描述"].Value = tbTaskDepict.Text;
            dgvTaskInfo.CurrentRow.Cells["任务备注"].Value = tbRemark.Text;
            btnSave.Visible = false;
            return result;
        }

        private void InitialdgvShop()
        //门店dgv初始化
        {
            DataGridViewButtonColumn Columnbc = new DataGridViewButtonColumn();
            Columnbc.DefaultCellStyle.NullValue = "X";
            Columnbc.HeaderText = "删除";
            Columnbc.Name = "删除";
            Columnbc.Width = 40;
            dgvShop.Columns.Add(Columnbc);

            DataTable dtTableInfo = ProjectCommon.ClassCommon.GetTableInfo(cc.scKengee_IT_MIS, "ShopInfo");
            for (int i = 0; i < dtTableInfo.Rows.Count; i++)
            {
                string FieldName = dtTableInfo.Rows[i]["字段名"].ToString();
                string FieldDescription = dtTableInfo.Rows[i]["字段说明"].ToString();
                int iColumnIndex = dgvShop.Columns.Add(FieldName, FieldDescription);
            }

            ProjectCommon.ClassCommon.SetdgvColumnsHide(dgvShop, ShopHideColumns0);
        }

        private void InitialdgvTaskInfo()
        //任务dgv初始化
        {
            //dgvTaskInfo.Columns.Add("X", "X");
            DataGridViewButtonColumn Columnbc = new DataGridViewButtonColumn();
            Columnbc.DefaultCellStyle.NullValue = "X";
            Columnbc.HeaderText = "删除";
            Columnbc.Name = "删除";
            Columnbc.Width = 40;
            dgvTaskInfo.Columns.Add(Columnbc);

            DataGridViewComboBoxColumn Columncbc = new DataGridViewComboBoxColumn();
            for(int i=0;i<gbTaskType.Controls.Count;i++)
            {
                Columncbc.Items.Add(gbTaskType.Controls[i].Text);
            }            
            Columncbc.HeaderText = "任务分类";
            Columncbc.Name = "任务分类";
            //Columncbc.AutoSizeMode=DataGridViewAutoSizeColumnMode.
            dgvTaskInfo.Columns.Add(Columncbc);
            dgvTaskInfo.Columns.Add("任务对象", "任务对象");
            dgvTaskInfo.Columns.Add("任务描述", "任务描述");
            dgvTaskInfo.Columns.Add("任务备注", "任务备注");
        }

        #region 快速录入
        private void GetFastInput(string TaskType)
        //得到快速录入的数据
        {
            string sqlObject = @"select TaskDepict from FastInput where (TaskType='" + TaskType + "')and(TaskObject='对象')";
            SqlDataAdapter sdaObject = new SqlDataAdapter(sqlObject, cc.scKengee_IT_MIS);
            DataTable dtObject = new DataTable();
            cc.scKengee_IT_MIS.Open();
            sdaObject.Fill(dtObject);
            cc.scKengee_IT_MIS.Close();
            sdaObject.Dispose();
            SetFastInput(dtObject, lvTaskObject);
            dtObject.Clear();
            dtObject.Dispose();

            string sqlDepict = @"select TaskDepict from FastInput where (TaskType='" + TaskType + "')and(TaskObject='描述')";
            SqlDataAdapter sdaDepict = new SqlDataAdapter(sqlDepict, cc.scKengee_IT_MIS);
            DataTable dtDepict = new DataTable();
            cc.scKengee_IT_MIS.Open();
            sdaDepict.Fill(dtDepict);
            cc.scKengee_IT_MIS.Close();
            sdaDepict.Dispose();
            SetFastInput(dtDepict, lvTaskDepict);
            dtDepict.Clear();
            dtDepict.Dispose();
        }

        private void SetFastInput(DataTable dtFastInput,ListView lvFastInput)
        //设置ListView中的快速录入数据
        {
            lvFastInput.Items.Clear();
            for (int i = 0; i < dtFastInput.Rows.Count; i++)
            {
                string sFastInput = dtFastInput.Rows[i][0].ToString();
                lvFastInput.Items.Add(sFastInput);
            }
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (lvTaskObject.SelectedItems.Count > 0)
            {
                dgvTaskInfo.CurrentRow.Cells["任务对象"].Value = lvTaskObject.SelectedItems[0].Text;                
            }
        }

        private void listView2_DoubleClick(object sender, EventArgs e)
        {
            if (lvTaskDepict.SelectedItems.Count > 0)
            {
                dgvTaskInfo.CurrentRow.Cells["任务描述"].Value = lvTaskDepict.SelectedItems[0].Text;
            }
        }
        #endregion 

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Abort;
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (dgvShop.RowCount < 1)
            {
                MessageBox.Show("没有选择门店");
            }
            else
            {
                this.DialogResult = DialogResult.OK;
                switch (ccosState)
                {
                    case ProjectCommon.ClassCommon.OperateState.osNew: JobTaskAppend(); break;
                    case ProjectCommon.ClassCommon.OperateState.osModify: JobTaskModify(); break;
                    case ProjectCommon.ClassCommon.OperateState.osView: { }; break;
                }
                this.Close();
            }
        }

        #region 门店信息
        private void GetShopFastCode()
        //获取门店快速代码
        {            
            if (tbShopFastCode.Text.Trim().Length < 1)
            {
                ProjectShop.FrmTaskShop ftoc = new ProjectShop.FrmTaskShop(cc);
                ftoc.ShowDialog();
                if (ftoc.DialogResult == DialogResult.OK)
                {
                    tbShopFastCode.Text = ftoc.Text;
                }
                ftoc.Dispose();
            } 
        }

        private bool ExistdgvShop(string FastCode)
        //根据代码，判断门店是否已经存在
        {
            bool result = false;
            for (int i = 0; i < dgvShop.RowCount; i++)
            {
                string FastCodeCurrentRow = dgvShop.Rows[i].Cells["tbFastCode"].Value.ToString();
                if(FastCode.Equals(FastCodeCurrentRow))
                {
                    dgvShop.CurrentCell = dgvShop.Rows[i].Cells["tbFastCode"];
                    result = true;
                    break;
                }
            }
            return result;
        }

        private void SetShop2dgv_FastCode(string FastCode)
        //添加门店信息
        {
            if ((FastCode.Length > 0) && (ExistdgvShop(FastCode)==false))
            {
                ProjectShop.ClassShop csShop = new ProjectShop.ClassShop(cc.scKengee_IT_MIS);
                if (csShop.GetShop_FastCode(FastCode))
                {
                    int NewRowIndex = dgvShop.Rows.Add();
                    for (int i = 0; i < csShop.dtShop.Columns.Count; i++)
                    {
                        dgvShop.Rows[NewRowIndex].Cells[i+1].Value = csShop.dtShop.Rows[0][i].ToString();
                    }                    
                    tbShopFastCode.Text = string.Empty;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        //选择门店
        {
            GetShopFastCode();
            SetShop2dgv_FastCode(tbShopFastCode.Text.Trim());
            
        }

        private void button3_Click(object sender, EventArgs e)
        //删除门店
        {
            dgvShop.Rows.Clear();
        }

        private void button1_Click(object sender, EventArgs e)
        //添加所有门店
        {
            string sql = @"select * 
                            from ShopInfo ";
            SqlDataAdapter sda = new SqlDataAdapter(sql, cc.scKengee_IT_MIS);
            DataTable dtShopInfo = new DataTable();
            cc.scKengee_IT_MIS.Open();
            sda.Fill(dtShopInfo);
            cc.scKengee_IT_MIS.Close();
            sda.Dispose();

            dgvShop.Rows.Clear();
            dgvShop.Rows.Add(dtShopInfo.Rows.Count);
            for (int i = 0; i < dtShopInfo.Rows.Count; i++)
            {
                for (int j = 0; j < dtShopInfo.Columns.Count; j++)
                {
                    string ColumnName = dtShopInfo.Columns[j].ColumnName;
                    
                    dgvShop.Rows[i].Cells[ColumnName].Value = dtShopInfo.Rows[i][ColumnName].ToString();
                }
            }
        }
        #endregion

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            string sPowerRight = cc.dtLoginUser.Rows[0]["PowerRight"].ToString();

            if ((sender as CheckBox).Checked)
            {
                if (sPowerRight[1] == '0')
                {
                    ProjectCommon.ClassCommon.SetdgvColumnsHide(dgvShop, "商户号,终端号");
                }
                else
                {
                    ProjectCommon.ClassCommon.SetdgvColumnsHide(dgvShop, string.Empty);
                }
            }
            else
            {
                ProjectCommon.ClassCommon.SetdgvColumnsHide(dgvShop, ShopHideColumns0);
            }
        }

        private void dgvShop_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string sFastCode = dgvShop.CurrentRow.Cells["tbFastCode"].Value.ToString();
            ProjectShop.FrmShopInfo fsi = new ProjectShop.FrmShopInfo(cc, ProjectCommon.ClassCommon.OperateState.osView, sFastCode);
            fsi.ShowDialog();
            fsi.Dispose();
        }

        private void tbFastCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btnChoice.PerformClick();
            }
        }

        private int JobTaskAppend()
        {
            int result = 0;
            for (int i = 0; i < dgvShop.Rows.Count; i++)
            {
                for (int j = 0; j < dgvTaskInfo.Rows.Count; j++)
                {
                    tbTaskType.Text = dgvTaskInfo.Rows[j].Cells["任务分类"].Value.ToString().Trim();
                    tbTaskObject.Text = dgvTaskInfo.Rows[j].Cells["任务对象"].Value.ToString().Trim();
                    tbTaskDepict.Text = dgvTaskInfo.Rows[j].Cells["任务描述"].Value.ToString().Trim();
                    if (dgvTaskInfo.Rows[j].Cells["任务备注"].Value != null)
                    {
                        tbRemark.Text = dgvTaskInfo.Rows[j].Cells["任务备注"].Value.ToString().Trim();
                    }

                    if ((tbTaskType.Text.Length > 0) && (tbTaskObject.Text.Length > 0) && (tbTaskDepict.Text.Length > 0))
                    {
                        tbShopFastCode.Text = dgvShop.Rows[i].Cells["tbFastCode"].Value.ToString();
                        ProjectCommon.ClassCommon.SetForm2Class(ctTask.dtTask, this.Controls);
                        result += ctTask.TaskAppend();
                    }
                }
            }
            return result;            
        }

        private int JobTaskModify()
        {
            int result = 0;
            tbTaskType.Text = dgvTaskInfo.CurrentRow.Cells["任务分类"].Value.ToString().Trim();
            tbTaskObject.Text = dgvTaskInfo.CurrentRow.Cells["任务对象"].Value.ToString().Trim();
            tbTaskDepict.Text = dgvTaskInfo.CurrentRow.Cells["任务描述"].Value.ToString().Trim();
            tbRemark.Text = dgvTaskInfo.CurrentRow.Cells["任务备注"].Value.ToString().Trim();
            if ((tbTaskType.Text.Length > 0) && (tbTaskObject.Text.Length > 0) && (tbTaskDepict.Text.Length > 0))
            {
                tbShopFastCode.Text = dgvShop.CurrentRow.Cells["tbFastCode"].Value.ToString();
                ProjectCommon.ClassCommon.SetForm2Class(ctTask.dtTask, this.Controls);
                result += ctTask.TaskModify();
            }
            return result;
        }

        private void dtpPlannedFinishDate_ValueChanged(object sender, EventArgs e)
        {
            tbPlannedFinishDate.Text = dtpPlannedFinishDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
        }

        private void FrmTask_Shown(object sender, EventArgs e)
        {
            InitialdgvShop();
            InitialdgvTaskInfo();
            switch (ccosState)
            {
                case ProjectCommon.ClassCommon.OperateState.osNew: TaskNewInitial(); break;
                case ProjectCommon.ClassCommon.OperateState.osModify: TaskModifyInitial(sTaskID); break;
                case ProjectCommon.ClassCommon.OperateState.osView: TaskViewInitial(sTaskID); break;
            }
            ProjectCommon.ClassCommon.CopyComboBoxItem(ctTask.cbTaskState, cbTaskState);

        }

        private void btnTaskType1_Click(object sender, EventArgs e)
        {
            if (ccosState == ProjectCommon.ClassCommon.OperateState.osNew)
            {
                int NewRowIndex = dgvTaskInfo.Rows.Add();
                dgvTaskInfo.CurrentCell = dgvTaskInfo.Rows[NewRowIndex].Cells["任务分类"];
            }
            string sTaskType = (sender as Button).Text;
            dgvTaskInfo.CurrentRow.Cells["任务分类"].Value = sTaskType;
            GetFastInput(sTaskType);
        }

        private void dgvTaskInfo_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex > -1)
            {
                string sColumnName = dgvTaskInfo.Columns[e.ColumnIndex].Name;
                if (sColumnName.Equals("删除"))
                {
                    dgvTaskInfo.Rows.RemoveAt(e.RowIndex);
                }
            }
        }

        private void dgvShop_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex > -1)
            {
                string sColumnName = dgvShop.Columns[e.ColumnIndex].Name;
                if (sColumnName.Equals("删除"))
                {
                    dgvShop.Rows.RemoveAt(e.RowIndex);
                }
            }
        }
    }
}
